#!/usr/bin/env bash

init_ps="`ps -p 1 -o comm=`"
init_path="`which $init_ps`"
init_type="`readlink ${init_path}`"

# OpenRC
sysman_for_openrc() {
    case "$1" in
        "enable")
            sudo rc-update add "$2" default;;
        "disable")
            sudo rc-update del "$2" default;;
        "start")
            sudo rc-service "$2" start;;
        "restart")
            sudo rc-service "$2" restart;;
        "stop")
            sudo rc-service "$2" stop;;
        "status")
            rc-status default | grep "$2";;
        *)
            echo "$1 is not a valid command.";;
    esac
}

# Runit
sysman_for_runit() {
    case "$1" in
        "enable")
            if [ ! -e "/etc/runit/sv/$2" ]; then
                sudo ln -s "/etc/runit/sv/$2" /run/runit/service
            fi 
            ;;
        "disable")
            if [ -e "/etc/runit/sv/$2" ]; then
                sudo unlink "/run/runit/service/$2"
                sudo rm -rf "/run/runit/service/$2"
            fi 
            ;;
        "start")
            sudo sv start "$2";;
        "restart")
            sudo sv restart "$2";;
        "stop")
            sudo sv stop "$2";;
        "status")
            sv status "$2";;
        *)
            echo "$1 is not a valid command.";;
    esac
}

# S6
sysman_for_s6() {
    case "$1" in
        "enable")
            sudo touch "/etc/s6/adminsv/default/contents.d/$2";;
        "disable")
            sudo rm -f "/etc/s6/adminsv/default/contents.d/$2";;
        "start")
            sudo s6-rc -u change "/run/service/$2";;
        "restart")
            sudo s6-rc -r change "/run/service/$2";;
        "stop")
            sudo s6-rc -d change "/run/service/$2";;
        "status")
            s6-svstat "/run/service/$2";;
        *)
            echo "$1 is not a valid command.";;
    esac
    sudo s6-db-reload
}

# Dinit
sysman_for_dinit() {
    case "$1" in
        "enable")
            sudo dinitctl enable service "$2";;
        "disable")
            sudo dinitctl disable service "$2";;
        "start")
            sudo dinitctl start "$2";;
        "restart")
            sudo dinitctl restart "$2";;
        "stop")
            sudo dinitctl stop "$2";;
        "status")
            sudo dinitctl status "$2";;
        "list")
            dinitctl list;;
        *)
            echo "$1 is not a valid command.";;
    esac
}

# SystemV
sysman_for_sysvinit() {
    case "$1" in
        "enable")
            sudo sh -c "cat > /etc/init/${1}.conf" <<EOF
# 在哪种运行级别下启动服务
start on filesystem or runlevel [2345]
# 在哪种运行级别下停止服务
stop on runlevel [!2345]
# 自动重启
respawn
# 启动命令
exec /usr/bin/dockerd -H unix:///var/run/docker.sock
EOF
            ;;
        "disable")
            sudo rm -rf "/etc/init/${1}.conf";;
        "start")
            sudo initctl start "$2";;
        "restart")
            sudo initctl restart "$2";;
        "stop")
            sudo initctl stop "$2";;
        "status")
            sudo initctl status "$2";;
        "list")
            initctl list;;
        *)
            echo "$1 is not a valid command.";;
    esac
    sudo initctl reload-configuration
}

echo "init type: $init_type"
# main
case "$init_type" in
    "systemd")
        systemctl $@;;
    "openrc-init")
        sysman_for_openrc $@;;
    "runit-init")
        sysman_for_runit $@;;
    "s6-init")
        sysman_for_s6 $@;;
    "dint-init")
        sysman_for_dint $@;;
    *)
        echo "Unsupported init type: $1";;
esac


